package org.atm.dao;

import com.alibaba.druid.util.JdbcUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;


public class DbUtil {


	protected static PreparedStatement ps = null;
	protected static ResultSet rs = null;
	protected static Connection conn = null;


	public static synchronized Connection getConn() {
		try {
//			URL resourceUrl = DbUtil.class.getClassLoader().getResource("druid.properties");
//			File file = new File(resourceUrl.toURI());
//			InputStream is = new FileInputStream(file);
//			Properties p = new Properties();
//			p.load(is);

			String url = "jdbc:mysql://121.43.52.97:3306/cstatm-mte?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";
			String user = "root";
			String password = "123456";
			String DriverClass = "com.mysql.cj.jdbc.Driver";

			Class.forName(DriverClass);

			Connection con = DriverManager.getConnection(url, user, password);
			return con;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static void close() {
		try {
			if (rs != null)
				rs.close();
			if (ps != null)
				ps.close();
			if (conn != null)
				conn.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static PreparedStatement executePreparedStatement(String sql) {
		try {
			ps = getConn().prepareStatement(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ps;
	}

	public static ResultSet executeQuery(String sql) {

		try {
			ps = executePreparedStatement(sql);
			rs = ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	public static void executeUpdate(String sql) {
		try {
			ps = executePreparedStatement(sql);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}// end DbUtil
